package com.renjibo.childred.adapter;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.renjibo.childred.MainActivity;
import com.renjibo.childred.R;
import com.renjibo.childred.fragments.DetailFragment;
import com.renjibo.childred.modle.TimeScareBean;
import com.renjibo.childred.servers.LongRunningServer;
import com.renjibo.childred.utils.TimeUtils;

import java.util.List;

/**
 * 任继波
 * Created by Administrator on 2017/2/23.
 */

public class MyAdapter extends BaseAdapter {
    private Context context;
    private final LayoutInflater inflater;
    private List<TimeScareBean.ProductlistBean> productlist;
    private ViewHolder viewHolder;

    public MyAdapter(Context context,List<TimeScareBean.ProductlistBean> productlist){
        this.context=context;
        this.productlist=productlist;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return productlist.size();
    }

    @Override
    public Object getItem(int position) {
        return productlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
                convertView=inflater.inflate(R.layout.listview_item,parent,false);
                viewHolder =new ViewHolder();
                viewHolder.scare_listView_title= (TextView) convertView.findViewById(R.id.scare_listView_title);
                viewHolder.scare_listView_newPrice= (TextView) convertView.findViewById(R.id.scare_listView_newPrice);
                viewHolder.scare_listView_oldPrice= (TextView) convertView.findViewById(R.id.scare_listView_oldPrice);
                viewHolder.scare_listView_surplusTime= (TextView) convertView.findViewById(R.id.scare_listView_surplusTime);
                viewHolder.scare_listView_Time= (TextView) convertView.findViewById(R.id.scare_listView_Time);
                viewHolder.scare_listView_button= (Button) convertView.findViewById(R.id.scare_listView_button);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
        TimeScareBean.ProductlistBean productlistBean = productlist.get(position);
        viewHolder.scare_listView_title.setText(productlistBean.getName()+"");
        viewHolder.scare_listView_newPrice.setText(productlistBean.getLimitprice()+"");
//
//        CountDownTimer timer = new CountDownTimer(10000, 1000) {
//
//            @Override
//            public void onTick(long millisUntilFinished) {
//                viewHolder.scare_listView_oldPrice.setText("剩余时间:"+millisUntilFinished);
//            }
//
//            @Override
//            public void onFinish() {
//
//            }
//        };
//        timer.start();

        viewHolder.scare_listView_surplusTime.setText(productlistBean.getLefttime()+"");
//        viewHolder.scare_listView_Time
        viewHolder.scare_listView_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"你点击了按钮",Toast.LENGTH_SHORT).show();
                MainActivity context = (MainActivity) MyAdapter.this.context;
                context.getSupportFragmentManager().beginTransaction().addToBackStack("fra").replace(R.id.tabHost, DetailFragment.newInstance("http://120.27.126.103:8080/ECServer_D/product","")).commit();
            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView scare_listView_title;
        TextView scare_listView_newPrice;
        TextView scare_listView_oldPrice;
        TextView scare_listView_surplusTime;
        TextView scare_listView_Time;
        Button scare_listView_button;
    }
}
